//
//  JSBaseCollectionViewController.m
//  NiubilityProject
//
//  Created by Apple on 2018/3/16.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSBaseCollectionViewController.h"



NSString *const JSCollectionViewCellIdentifier = @"JSBaseCollectionCell";
//NSString *const QMUICommonTableViewControllerSectionFooterIdentifier = @"QMUISectionFooterView";
@interface JSBaseCollectionViewController ()

@end

@implementation JSBaseCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (!objc_getAssociatedObject(self, _cmd)) {
        if ([self isShowLoigngView]) {
            [JSLoadingView showLoadingInView:self.view];
            [self headerRefresh];
        }
        objc_setAssociatedObject(self, @selector(viewWillAppear:), @(YES), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        self.view.backgroundColor = JS_colorWithHex(kBodyBGColor);
    }
}

-(instancetype) initWithLayout:(UICollectionViewLayout *) layout{
    if (self = [super init]) {
        _curLayout = layout;
    }
    return self;
}
- (void)didInitialized{
    [super didInitialized];
    // 1.注册cell or header footer
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:JSCollectionViewCellIdentifier];
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.dataService = [[JSBaseDataService alloc] init];
    self.dataService.delegate = self;
    
    self.collectionView.emptyDataSetSource = self;
    self.collectionView.emptyDataSetDelegate = self;
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.collectionView.mj_header = [CustomGifRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    // 自动改变透明度
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    //    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.collectionView.mj_footer = [CustomGifRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.collectionView.mj_footer.automaticallyChangeAlpha = YES;
    _isHidenHeaderRefresh = YES;
    _isHidenFooterRefresh = YES;
    [self.collectionView.mj_footer endRefreshingWithNoMoreData];
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        if (!self.curLayout) {
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.scrollDirection = UICollectionViewScrollDirectionVertical;
            _curLayout = layout;
            //这个是水平布局
            //layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        }
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.curLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _collectionView;
}
- (void)headerRefresh{
    [self.dataService refrenshData];
}
- (void)footerRefresh{
    [self.dataService nextData];
}

- (void)setIsHidenFooterRefresh:(BOOL)isHidenFooterRefresh{
    _isHidenFooterRefresh = isHidenFooterRefresh;
    if (isHidenFooterRefresh) {
        self.collectionView.mj_footer = nil;
    }else{
//        self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        self.collectionView.mj_footer = [CustomGifRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];

        self.collectionView.mj_footer.automaticallyChangeAlpha = YES;
    }
}
- (void)setIsHidenHeaderRefresh:(BOOL)isHidenHeaderRefresh{
    _isHidenHeaderRefresh = isHidenHeaderRefresh;
    if (_isHidenHeaderRefresh) {
        self.collectionView.mj_header = nil;
    }else{
//        self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
        self.collectionView.mj_header = [CustomGifRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];

        // 自动改变透明度
        self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    }
}
- (void)footerRefreshType{
    if (self.dataSource.count < self.dataService.page * 10) {
        [self.collectionView.mj_footer resetNoMoreData];
    }else{
        [self.collectionView.mj_footer endRefreshingWithNoMoreData];
    }
}
- (void)layoutSubView{
    
}
#pragma mark - UICollectionViewDataSource - UICollectionViewDelegate -
//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
//返回每个item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:JSCollectionViewCellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return cell;
}

//设置每个item的大小，双数的为50*50 单数的为100*100
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row%2==0) {
        return CGSizeMake(50, 50);
    }else{
        return CGSizeMake(100, 100);
    }
}
//对头视图或者尾视图进行设置

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
//动态设置每个分区的EdgeInsets

//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

//动态设置每行的间距大小

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return kRealValue(10);
}



//动态设置每列的间距大小

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return kRealValue(10);
}



//动态设置某个分区头视图大小

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;



//动态设置某个分区尾视图大小

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

#pragma mark - DZNEmptyDataSetSource
// 空白页图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"icon_empty"];
}
// 标题文本，富文本样式
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"暂无数据  ";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: JS_ColorWithRGB(222, 222, 222)};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

// 是否 允许上下滚动，默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

#pragma mark -------JSDataServiceDelegate-----------------
// 请求数据回调
-(void)dataServiceDidUpdate:(JSBaseDataService *)service data:(id)data error:(NSError *)error{
    NSLog(@"data:%@ -- error:%@", data,error);
    [JSLoadingView hidenLoadingForView:self.view];
    //    [self footerRefreshType];
}
-(NSString *)dataServiceUrlSting{
    // 请求所需url
    return BASE_URL;
}
-(NSMutableDictionary *)dataServiceParameter{
    // 请求所需参数
    return @{}.mutableCopy;
}
// 结束下拉刷新
-(void)overHeaderRefrensh{
    [self.collectionView.mj_header endRefreshing];
}
/*!
 结束上拉加载更多
 
 isResstingData : 是否重置数据
 */
-(void)overFooterRefrensh:(BOOL)isResstingData{
    if (isResstingData) {
        NSMutableArray *temp = self.dataSource;
        [self.dataSource removeAllObjects];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [temp class];
        });
    }
    [self.collectionView.mj_footer endRefreshing];
}
-(NSInteger)dataServicePageNumber{
    return 0;
}
- (BOOL)isShowLoigngView{
    return YES;
}
/** 数据源 */
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    id temp = objc_getAssociatedObject(self, _cmd);
    if (!temp) {
        [self layoutSubView];
        objc_setAssociatedObject(self, @selector(viewDidLayoutSubviews), @(YES), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
- (void)dealloc{
    JSLog(@"控制器销毁");
}
@end
